<?php
/*
Plugin Name: SlopeTracker Flickr Toolbox
Author: SlopeTracker
Author URI: http://pluginexpert.com
Plugin URI: http://pluginexpert.com
Description: Description for plugin.
Version: 1.0
This plugin inherits the GPL license from it's parent system, WordPress.
*/
/*-----------------------------------------------------------------------------------*/
/* Start Plugin Functions - Please refrain from editing this file */
/*-----------------------------------------------------------------------------------*/

class stFlickrToolbox {
     public $config = array ();
    public $addOn = array ();

	public function __construct($config) {
            $this->config = $config;
            if(empty($this->config ['pluginDir']))
                $this->config ['pluginDir'] = WP_PLUGIN_DIR . '/' . $config ['plugin_name'] . '/';
            if(empty($this->config ['pluginPath']))
                $this->config ['pluginPath'] = WP_PLUGIN_URL . '/' . $config ['plugin_name'] . '/';
            $this->getAddOnConfig();

            if(!is_admin ()){
                $method = array($this, 'frontEndExcute');
                add_action('after_setup_theme', $method);
            }else{
                $method = array($this, 'backEndExcute');
                add_action( 'admin_menu', $method);
            }
            $this->loadLib();
            $this->createAddOn();
            $hookName = $this->config['plugin_name'] . "Hook";
            $hook = new $hookName($this->config);
	}

        public function createAddOn(){
            if(count($this->config['addOn']) > 0 ){
                $this->haveSubMenu = false;
                foreach ($this->config['addOn'] as $config) {
                    $option = $this->option = get_option($this->config['plugin_name'].'_'.$this->config['option_key'].'_settings');
                    if($option['addons'][$config['plugin_name']]){
                        $fileDir = $this->config ['fileDir'];
                        require_once $fileDir . DIRECTORY_SEPARATOR . $this->config['addOnFolder'].DIRECTORY_SEPARATOR.$config['plugin_name'].DIRECTORY_SEPARATOR.$config['plugin_name'].'.php';
                        $config['pluginDir'] = $this->config['pluginDir'].$this->config['addOnFolder'].'/'.$config['plugin_name'].'/';
                        $config['fileDir'] = $this->config['fileDir']. DIRECTORY_SEPARATOR .$this->config['addOnFolder']. DIRECTORY_SEPARATOR .$config['plugin_name'];
                        $config['pluginPath'] = $this->config['pluginPath'].$this->config['addOnFolder']. '/' .$config['plugin_name'] . '/';
                        $config['subMenu'] = true;
                        $config['parentPage'] = $this->config['plugin_name'];
                        $config['disable_tabs'] = $this->getDisableTabs();
                        $config = $this->setDefaultAction($config);
                        //var_dump($config['default_action']);
                        $this->addOn[] = $pluginClass = new $config['plugin_name']($config);
                        $this->haveSubMenu = true;
                    }

                }
            }
        }

        public function getDisableTabs(){
            $childTabs = $this->config['child_tabs'];
            if(is_array($childTabs)){
                $disableKey = array_keys($childTabs);
                foreach ($childTabs as $key => $childTab) {
                    if(!array_key_exists('title', $childTab)){
                        return array_merge($disableKey, array_keys($childTab));
                    }
                }
            }
        }

        public function setDefaultAction($config){
            if(is_array($config['disable_tabs']) && in_array($config['default_action'], $config['disable_tabs'])){
                $diff_array = array_diff(array_keys($config['admin_tabs']), $config['disable_tabs']);
                $config['default_action'] = array_shift($diff_array);
            }
            return $config;
        }

        public function getAddOnConfig(){
            if(!empty($this->config['addOnFolder'])){
                $addOnFolder = $this->config['pluginDir'].$this->config['addOnFolder'];
                $folder_array = @scandir($addOnFolder);
                if($folder_array){
                    foreach ($folder_array as $folder) {
                        if($folder[0] != '.'){
                            $config_tmp = include $addOnFolder . '/'. $folder . '/config.php';
                            $this->config['addOn'][] = $config_tmp;
                        }
                    }
                }
            }
        }

        public function frontEndExcute() {
            $this->addFrontendScript();
            if ($_REQUEST['page'] == $this->config['plugin_name'] && $_REQUEST ['ajax'] == 1) {
                $this->ajaxAction();
            }else{
                $this->excute();
            }
	}

        public function addFrontendScript() {
            $config = $this->config;

            $controllerName = $config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($config);

            $method = array($controllerObject, 'loadCss' );
            add_action( "wp_print_styles", $method );
            $method = array($controllerObject, 'loadScript' );
            add_action( "wp_print_scripts", $method );
        }

        public function backEndExcute() {
            $icon = WP_PLUGIN_URL.'/'.$this->config['plugin_name'].'/images/slope-icon.png';
            $method = array ($this, 'excute' );
            if(count($this->config['addOn']) > 0 && $this->haveSubMenu ){
                add_submenu_page($this->config['plugin_name'], $this->config ['page_title'], $this->config ['plugin_menu_title'], $this->config['permission_capabilitie'], $this->config ['plugin_name'], $method);
            }
            if ($this->config['subMenu']) {
                $this->pluginPage = add_submenu_page($this->config['parentPage'], $this->config ['page_title'], $this->config ['plugin_menu_title'], $this->config['permission_capabilitie'], $this->config ['plugin_name'], $method);
            } else {
                $this->pluginPage = add_menu_page ( $this->config ['page_title'], $this->config ['plugin_menu_title'], $this->config['permission_capabilitie'], $this->config ['plugin_name'], $method, $icon );
            }

            if($_REQUEST['page'] == $this->config['plugin_name']){
                $this->addBackendScript();
                if ($_REQUEST ['ajax'] == 1) {
                    $this->ajaxAction();
                }
            }
	}

        public function addBackendScript() {

            $config = $this->config;

            $controllerName = $config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($config);
            $method = array($controllerObject, 'head' );
            add_action( "admin_print_scripts-$this->pluginPage", $method );
            $method = array($controllerObject, 'customHead' );
            add_action( "admin_print_scripts-$this->pluginPage", $method );
            unset($controllerObject);
        }

        public function loadLib(){
            $fileDir = $this->config ['fileDir'];
            $verstion = $this->config ['plugin_version'];
            require_once $fileDir . '/libs/' . $verstion . '/classes/stcontroller.php';
            require_once $fileDir . '/libs/' . $verstion . '/classes/stmodel.php';
            require_once $fileDir . '/libs/' . $verstion . '/classes/stview.php';
            require_once $fileDir . '/libs/' . $verstion . '/classes/sthook.php';
            require_once $fileDir . '/libs/' . $verstion . '/classes/stfunctions.php';
            require_once $fileDir . '/libs/' . $verstion . '/functions/core.php';
            require_once $fileDir . '/functions.php';
            require_once $fileDir . '/hook.php';
            require_once $fileDir . '/controller.php';
//            require_once $fileDir . '/stlicense.php';
        }

        public function excute() {
            $config = $this->config;
            $config['current_action'] = $this->getAction();
            $controllerName = $config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($config);
            $controllerObject->registerView();
            $controllerObject->add_admin();

            do_action($config['plugin_name'] . '_extra_contruction');
	}

        public function ajaxAction() {
            $config = $this->config;
            $config['current_action'] = $this->getAction();
            $controllerName = $config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($config);
            $controllerObject->registerView();
            $url = $_SERVER['REQUEST_URI'];
            $url = explode('?page=', $url);
            if(count($url) > 1){
                if($config['subMenu'] && $url[1] != $config['plugin_name']){
                     $controllerObject->loadSourceSubAdmin();
                }
            }

            $controllerObject->ajax_action();

            do_action($config['plugin_name'] . '_extra_contruction');
	}

        public function activationPlugin() {
            $config = $this->config;
            $controllerName = $config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($config);
            $controllerObject->activationPlugin();
            foreach ($this->addOn as $key => $addOn) {
                $addOn->activationPlugin();
            }
        }

        public function deactivationPlugin() {
            $config = $this->config;
            $controllerName = $config['plugin_name'] . "Controller";
            $controllerObject = new $controllerName($config);
            $controllerObject->deactivationPlugin();

            foreach ($this->addOn as $key => $addOn) {
                $addOn->deactivationPlugin();
            }
        }

        public function getAction(){

            if (isset ( $_REQUEST ['action'] )) {
                return $_REQUEST ['action'];
            }elseif(is_admin ()){
                return $this->config['default_action'];
            }elseif ( get_option('permalink_structure') != '' && !is_admin() ) {
                $url = $_SERVER['REQUEST_URI'];

                $home_url = get_home_url('/');

                $parts = parse_url($home_url);
                /*** return the host domain ***/
                $url = $parts['scheme'].'://'.$parts['host'].$url;

                $url = str_replace($home_url, '', $url);
                $url_tmp = explode('/', $url);
                foreach ($url_tmp as $value) {
                    if($value != ''){
                        $title_tmp = $value;
                        break;
                    }
                }

                if($title_tmp != '') {
                    return $title_tmp;
                }
            }
            return false;
        }
}

$fileDir = dirname(__FILE__);
$config = include $fileDir . '/config.php';
$subDir = explode('wp-content'.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR, $fileDir);
$isSubDir = $subDir[1] == $config['plugin_name']? false:true;
if(!$isSubDir){
    $config['fileDir'] = $fileDir;
    $pluginClass = new $config['plugin_name']($config);

    $method = array($pluginClass, 'activationPlugin');
    register_activation_hook(__FILE__, $method);
    $method = array($pluginClass, 'deactivationPlugin');
    register_deactivation_hook(__FILE__, $method);
}
?>